package com.imooc.sell.controller;


import com.imooc.sell.VO.ProductInfoVo;
import com.imooc.sell.VO.ProductVo;
import com.imooc.sell.VO.ResultVo;
import com.imooc.sell.dataobject.ProductCategory;
import com.imooc.sell.dataobject.ProductInfo;
import com.imooc.sell.sevice.ProductCategoryService;
import com.imooc.sell.sevice.ProductService;
import com.imooc.sell.utils.ResultVoUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/buyer/product")
public class BuyProductController {


    @Autowired
    private ProductService productService;

    @Autowired
    private ProductCategoryService categoryService;

    @GetMapping("/list")
    public ResultVo list() {

        //1 查询 所有上架的商品123
        List<ProductInfo> productInfoList = productService.findUpAll();

        //2 查询类目
        List<Integer> categoryTypeList = new ArrayList<>();

        List<Integer> temp = productInfoList.stream().map(u ->u.getCategoryType()).collect(Collectors.toList());

        if(temp != null){
            categoryTypeList.addAll(temp);
        }

        //类目结构
        List<ProductCategory>  productCategoryList = categoryService.findProductCategoriesByCategoryTypeIn(categoryTypeList);

        //数据拼接
        List<ProductVo> productVoList = new ArrayList<>();
        for (ProductCategory category:productCategoryList){

            ProductVo productVo = new ProductVo();
            productVo.setCategoryName(category.getCategoryName());
            productVo.setCategoryType(category.getCategoryType());

            List<ProductInfoVo> productInfoVoList = new ArrayList<>();
            for (ProductInfo productInfo:productInfoList){
                if(productInfo.getCategoryType() == category.getCategoryType()){
                    ProductInfoVo productInfoVo = new ProductInfoVo();
                    BeanUtils.copyProperties(productInfo,productInfoVo);
                    productInfoVoList.add(productInfoVo);
                }
            }
            productVo.setProductInfoVoList(productInfoVoList);
            productVoList.add(productVo);
        }

        return ResultVoUtil.success(productVoList);

    }
/*    public static void  main(String[] args){
        List<String> a = new ArrayList<>( );
        a.addAll(null);
        Assert.isTrue(true,"year");

    }*/

}
